package com.hx.resource.controller;

import com.hx.common.annotation.Log;
import com.hx.common.core.controller.BaseController;
import com.hx.common.core.domain.AjaxResult;
import com.hx.common.core.page.TableDataInfo;
import com.hx.common.enums.BusinessType;
import com.hx.common.utils.poi.ExcelUtil;
import com.hx.resource.domain.FileAcceptShare;
import com.hx.resource.domain.FileInfo;
import com.hx.resource.domain.FileShare;
import com.hx.resource.service.IFileAcceptShareService;
import com.hx.resource.service.IFileInfoService;
import com.hx.resource.service.IFileShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 接收文件分享Controller
 * 
 * @author mohao
 * @date 2023-05-19
 */
@RestController
@RequestMapping("/accept")
public class FileAcceptShareController extends BaseController
{
    @Autowired
    private IFileAcceptShareService fileAcceptShareService;
    @Autowired
    private IFileShareService fileShareService;
    @Autowired
    private IFileInfoService fileInfoService;

    /**
     * 查询接收文件分享列表
     */
    @PreAuthorize("@ss.hasPermi('resource:accept:list')")
    @GetMapping("/list")
    public TableDataInfo list(FileAcceptShare fileAcceptShare)
    {
        startPage();
        List<FileAcceptShare> list = fileAcceptShareService.selectFileAcceptShareList(fileAcceptShare);
        return getDataTable(list);
    }

    /**
     * 导出接收文件分享列表
     */
    @PreAuthorize("@ss.hasPermi('resource:accept:export')")
    @Log(title = "接收文件分享", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FileAcceptShare fileAcceptShare)
    {
        List<FileAcceptShare> list = fileAcceptShareService.selectFileAcceptShareList(fileAcceptShare);
        ExcelUtil<FileAcceptShare> util = new ExcelUtil<FileAcceptShare>(FileAcceptShare.class);
        util.exportExcel(response, list, "接收文件分享数据");
    }

    /**
     * 获取接收文件分享详细信息
     */
    @PreAuthorize("@ss.hasPermi('resource:accept:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(fileAcceptShareService.selectFileAcceptShareById(id));
    }

    /**
     * 新增接收文件分享
     */
    @PreAuthorize("@ss.hasPermi('resource:accept:add')")
    @Log(title = "接收文件分享", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @Transactional
    public AjaxResult add(@RequestBody FileShare fileShare)
    {
        Long userId = getUserId();//登录的用户ID
        List<FileShare> fileShares = fileShareService.selectFileShare(fileShare);
        System.out.println(fileShares);
        if (fileShares.size()==0) return error("链接或提取码无效");
        FileShare share = fileShares.get(0);
        System.out.println(share);
        if (share.getPower()==0) return error("链接已过期");
        FileAcceptShare fileAcceptShare = new FileAcceptShare();
        fileAcceptShare.setUserId(userId);
        fileAcceptShare.setFileId(share.getFileId());
        fileAcceptShare.setReceptionTime(new Date());
        Long count = share.getCount();
        count++;
        share.setCount(count);
        fileShareService.updateFileShare(share);
        fileAcceptShareService.insertFileAcceptShare(fileAcceptShare);
        //接收分享文件后存入文件列表
        FileInfo fileInfo = fileInfoService.selectFileInfoById(share.getFileId());
        fileInfo.setUserId(userId);
        fileInfo.setCrateDate(new Date());
        fileInfoService.insertFileInfo(fileInfo);
        return success("分享成功");
    }

    /**
     * 修改接收文件分享
     */
    @PreAuthorize("@ss.hasPermi('resource:accept:edit')")
    @Log(title = "修改文件分享", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FileAcceptShare fileAcceptShare)
    {
        return toAjax(fileAcceptShareService.updateFileAcceptShare(fileAcceptShare));
    }

    /**
     * 删除接收文件分享
     */
    @PreAuthorize("@ss.hasPermi('resource:accept:remove')")
    @Log(title = "接收文件分享", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(fileAcceptShareService.deleteFileAcceptShareByIds(ids));
    }
}
